package com.bkgy.controller.allprocess.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bkgy.controller.allprocess.dto.AllProcessIndexDetails;
import com.bkgy.controller.allprocess.dto.AllpricessIndexReasonRequst;
import com.bkgy.controller.allprocess.entity.TbAllpricessIndexEntity;
import com.bkgy.controller.allprocess.mapper.AllProcessIndexReasonMapper;
import com.bkgy.controller.allprocess.service.AllProcessIndexReasonService;
import com.bkgy.sys.common.entity.ResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service

public class AllProcessIndexReasonServiceImpl  implements AllProcessIndexReasonService {


    @Autowired
    private AllProcessIndexReasonMapper reasonMapper;

     /**
     * 全流程成材率模型首页数 异常原因 和 未完成原因
     * @param request_get AllpricessIndexReasonRequst
     * @return AllProcessIndexDetails
     */
    public  ResultData<AllProcessIndexDetails> getAllprocessIndexReason (AllpricessIndexReasonRequst request_get) {
        //日期转换与赋值
        AllpricessIndexReasonRequst request = setdatetime(request_get);
        try {
            Page<?> page = new Page<>(request.getPageIndex(), request.getPageSize());

            //总行数数
            int count = 0;

            // 构建响应对象
            AllProcessIndexDetails indexdetails = new AllProcessIndexDetails();


            //异常原因
            List<TbAllpricessIndexEntity> results_otherReason = new ArrayList<>();
            if(request.getLinename()==null)
            {
                request.setLinename("cal1630");
            }
//            if(request.getLinename().equals("all"))
//            {
//                results_otherReason= reasonMapper.getotherReasonAll(page, request);
//            }
            else if(request.getLinename().equals("cal1630"))
            {
                results_otherReason= reasonMapper.getotherReason1630(page, request);
                count=(int)page.getTotal();
            }
            else if(request.getLinename().equals("cal2150"))
            {
                results_otherReason= reasonMapper.getotherReason2150(page, request);
                count=(int)page.getTotal();
            }
            else if(request.getLinename().equals("cgl1870"))
            {
                results_otherReason= reasonMapper.getotherReason1870(page, request);
                count=(int)page.getTotal();
            }
            else if(request.getLinename().equals("tcm2280"))
            {
                results_otherReason= reasonMapper.getotherReason2280(page, request);
                count=(int)page.getTotal();
            }
            indexdetails.setOtherReason(results_otherReason);

            //未完成原因
            List<TbAllpricessIndexEntity> results_unCompleteReason = new ArrayList<>();
            TbAllpricessIndexEntity entity = new TbAllpricessIndexEntity();
            String unCompleteReason_str = "";
            if (!results_otherReason.isEmpty()) {
                if (results_otherReason.size() >= 4) {
                    for (int i = 0; i < 4; i++) {
                        unCompleteReason_str += results_otherReason.get(i).getUncompletereason().toString() + "。";
                    }
                } else {
                    for (int i = 0; i < results_otherReason.size(); i++) {
                        unCompleteReason_str += results_otherReason.get(i).getUncompletereason().toString() + "。";
                    }
                }
                entity.setUncompletereason(unCompleteReason_str);
                results_unCompleteReason.add(entity);
            }

            indexdetails.setUnCompleteReason(results_unCompleteReason);

            return ResultData.ResultDataSuccess(indexdetails, count);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultData.ResultDataFaultSelf("获取失败" + e.getMessage(), null);
        }
    }

    /**
     * 根据前台传入查询开始和查询结束日期，构造出三种时间格式以便后续表数据查询
     * @param request_get TbAllpricessIndexEntity
     * @return TbAllpricessIndexEntity
     */
    public AllpricessIndexReasonRequst setdatetime(AllpricessIndexReasonRequst request_get) {
        try {
            String[] array_startTime = request_get.getDbegin().split("-");//时间格式：2025-06-01 00:00:01
            String dbegin_Times = "";//时间格式：20250703075254
            if (array_startTime.length > 0) {
                //构建时间格式：20250703075254
                String times2 = array_startTime[2].replace(" ", "");
                times2 = times2.replace(":", "");
                dbegin_Times = array_startTime[0] + array_startTime[1] + times2;
            }
            request_get.setDbegin(dbegin_Times);//时间格式：20250703075254

            String[] array_endTime = request_get.getDend().split("-");//时间格式：2025-06-01 00:00:01
            String endTime_Times="";//时间格式：20250703075254
            if (array_endTime.length > 0) {
                //构建时间格式：20250703075254
                String times2=array_endTime[2].replace(" ","");
                times2 = times2.replace(":","");
                endTime_Times=array_endTime[0]+array_endTime[1]+times2;
            }
            request_get.setDend(endTime_Times);//时间格式：20250703075254

        } catch (Exception e) {
            e.printStackTrace();
        }
        return request_get;
    }

}